package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class SubAreaAction extends BaseAction<SubArea>{
	
	//注入service层
	@Autowired
	private SubAreaService subAreaService;
	
	//接受文件file 属性注入
	private File file;

	public void setFile(File file) {
		this.file = file;
	}
	//批量数据分区导入
	@Action(value="subArea_batchImport")
	public String subAreaBatchImport() throws IOException {
		List<SubArea> subAreasList=new ArrayList<SubArea>();
		//解析Excel文件
		HSSFWorkbook hssfWorkbook=new HSSFWorkbook(new FileInputStream(file));
		// 读取sheet的第一张表
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		//读取sheet的每一行
		for (Row row : sheet) {
			if(row.getRowNum()==0) {
				continue;
			}
			//跳出单元格空格
			if(row.getCell(0)==null||StringUtils.isBlank(row.getCell(0).getStringCellValue()) ) {
				continue;
			}
			SubArea subArea=new SubArea();
			subArea.setId(row.getCell(0).getStringCellValue());
			//subArea.setFixedArea(row.getCell(1).getStringCellValue());
			//subArea.setArea(row.getCell(2).getStringCellValue());
			subArea.setKeyWords(row.getCell(3).getStringCellValue());
			subArea.setStartNum(row.getCell(4).getStringCellValue());
			subArea.setEndNum(row.getCell(5).getStringCellValue());
			//subArea.setSingle(row.getCell(6).getStringCellValue());
			subArea.setAssistKeyWords(row.getCell(7).getStringCellValue());
			subAreasList.add(subArea);
		}
		//调用业务层
		subAreaService.saveExcel(subAreasList) ;
		return NONE;
	}
	
}
